package io.lkt.modules.sys.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import io.lkt.common.validator.ValidatorUtils;
import io.lkt.modules.sys.entity.SysGzhEntity;
import io.lkt.modules.sys.service.SysUserRoleService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.lkt.modules.sys.entity.BBusinessDictEntity;
import io.lkt.modules.sys.service.BBusinessDictService;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;



/**
 * 商学院字典表
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2020-10-23 18:17:52
 */
@RestController
@RequestMapping("/sys/bbusinessdict")
public class BBusinessDictController {
    @Autowired
    private BBusinessDictService bBusinessDictService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:bbusinessdict:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bBusinessDictService.queryPage(params);
        List<?> list = page.getList();

        if (CollectionUtils.isNotEmpty(list)) {
            List<BBusinessDictEntity> BusinessDictList = Lists.newArrayList();

            for (Object temp : list) {
                BBusinessDictEntity bBusinessDictEntity = (BBusinessDictEntity) temp;
                //公众号名称
                if ((bBusinessDictEntity.getGzhCode() != null) && (!bBusinessDictEntity.getGzhCode().equals(""))) {
                    SysGzhEntity sysGzh = this.sysUserRoleService.getGzhByCode(bBusinessDictEntity.getGzhCode());
                    bBusinessDictEntity.setGzhName(sysGzh.getGzhName());
                } else {
                    bBusinessDictEntity.setGzhName("全部平台");
                }

                BusinessDictList.add(bBusinessDictEntity);
            }
            page.setList(BusinessDictList);
        }
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/queryAll")
//    @RequiresPermissions("sys:bbusinessdict:queryAll")
    public R queryAll(@RequestBody BBusinessDictEntity bBusinessDictEntity){
        List<BBusinessDictEntity> bBusinessDict = bBusinessDictService.queryAll(bBusinessDictEntity);
        return R.ok().put("bBusinessDict", bBusinessDict);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sys:bbusinessdict:info")
    public R info(@PathVariable("id") Integer id){
        BBusinessDictEntity bBusinessDict = bBusinessDictService.getById(id);
        //公众号标识
        if (bBusinessDict.getGzhCode() == null || bBusinessDict.getGzhCode().equals("")) {
            bBusinessDict.setGzhCode("-1");
        }

        return R.ok().put("bBusinessDict", bBusinessDict);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:bbusinessdict:save")
    public R save(@RequestBody BBusinessDictEntity bBusinessDict){

        if (bBusinessDict.getGzhCode() == null || bBusinessDict.getGzhCode().equals("") || bBusinessDict.getGzhCode().equals("-1")) {
            bBusinessDict.setGzhCode("");
        }

        bBusinessDict.setCreateTime(new Date());
        bBusinessDict.setUpdateTime(new Date());
        bBusinessDictService.save(bBusinessDict);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:bbusinessdict:update")
    public R update(@RequestBody BBusinessDictEntity bBusinessDict){
        ValidatorUtils.validateEntity(bBusinessDict);

        if (bBusinessDict.getGzhCode() == null || bBusinessDict.getGzhCode().equals("") || bBusinessDict.getGzhCode().equals("-1")) {
            bBusinessDict.setGzhCode("");
        }

        bBusinessDict.setUpdateTime(new Date());
        bBusinessDictService.updateById(bBusinessDict);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:bbusinessdict:delete")
    public R delete(@RequestBody Integer[] ids){
        bBusinessDictService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
